﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using BUS;
using System.IO;
namespace GUI
{
    public partial class uc_PhuKien : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_PhuKien()
        {
            InitializeComponent();
        }

        private AccessoryBL accBL = new AccessoryBL();
        private AccesoryDTO acc = new AccesoryDTO();

        private int GetAccessoryInfo()
        {
            if (txtTenPK.Text.Length < 2)
            {
                MessageBox.Show("Vui lòng chọn phụ kiện", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return 0;
            }
            else
            {
                if (radioGroup1.SelectedIndex != 0)
                    acc.AccessoryID = int.Parse(txtMaDT.Text);
                acc.Name = txtTenPK.Text;
                acc.Prize = int.Parse(txtGiaBan.Text);
                acc.Type = cbbLoaiPK.Text;
                acc.WarrantyTime = int.Parse(txtBaoHanh.Text);
                acc.Brand = txtNhanHieu.Text;
                acc.Color = txtMauSac.Text;
                acc.Count = int.Parse(txtSoLuongTon.Text);
            }
            return 1;
            
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioGroup1.SelectedIndex == 0)
            {
                txtTenPK.Text = "";
                txtNhanHieu.Text = "";
                txtSoLuongTon.Text = "";
                txtGiaBan.Text = "";
                txtMauSac.Text = "";
                txtBaoHanh.Text = "";
                cbbLoaiPK.Text = "";
                txtTenPK.Enabled = true;
                txtNhanHieu.Enabled = true;
                txtSoLuongTon.Enabled = true;
                txtGiaBan.Enabled = true;
                txtMauSac.Enabled = true;
                txtBaoHanh.Enabled = true;
                cbbLoaiPK.Enabled = true;
            }
            else if (radioGroup1.SelectedIndex == 1)
            {
                txtMaDT.Enabled = true;
                txtTenPK.Enabled = true;
                txtNhanHieu.Enabled = true;
                txtSoLuongTon.Enabled = false;
                txtGiaBan.Enabled = true;
                txtMauSac.Enabled = true;
                txtBaoHanh.Enabled = true;
                cbbLoaiPK.Enabled = true;
            }
            else if (radioGroup1.SelectedIndex == 2)
            {
                txtMaDT.Enabled = true;
                txtTenPK.Enabled = false;
                txtNhanHieu.Enabled = false;
                txtSoLuongTon.Enabled = false;
                txtGiaBan.Enabled = false;
                txtMauSac.Enabled = false;
                txtBaoHanh.Enabled = false;
                cbbLoaiPK.Enabled = false;
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            txtTenPK.Text = "";
            txtNhanHieu.Text = "";
            txtSoLuongTon.Text = "";
            txtGiaBan.Text = "";
            txtMaDT.Text = "";
            txtMauSac.Text = "";
            txtBaoHanh.Text = "";
            cbbLoaiPK.Text = "";
        }

        private void uc_PhuKien_Load(object sender, EventArgs e)
        {
            accessoryTableAdapter.Fill(sellingmobilemanagementDataSet2.accessory);
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            txtMaDT.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "AccessoryID").ToString();
            txtTenPK.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Name").ToString();
            txtBaoHanh.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "WarrantyTime").ToString();
            txtGiaBan.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Prize").ToString();
            txtMauSac.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Color").ToString();
            txtSoLuongTon.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Count").ToString();
            txtNhanHieu.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Brand").ToString();
            cbbLoaiPK.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Type").ToString();
            int imageid = Int32.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ImageID").ToString());
            ImageBL imgbl = new ImageBL();
            ImageDTO imgdto = new ImageDTO();
            imgdto = imgbl.Image_SelectByID(imageid);
            if (imgdto.Image == null)
                picPhuKien.Image = null;
            else
            {
                MemoryStream st = new MemoryStream();
                st.Write(imgdto.Image, 0, imgdto.Image.Length);
                Image i = Image.FromStream(st);
                picPhuKien.Image = resizeImage(i, picPhuKien.Width, picPhuKien.Height);
            }

        }
        public Image resizeImage(Image img, int width, int height)
        {
            Bitmap b = new Bitmap(width, height);
            Graphics g = Graphics.FromImage((Image)b);

            g.DrawImage(img, 0, 0, width, height);
            g.Dispose();

            return (Image)b;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Xác Nhận Cập Nhật?", "Confirmation", MessageBoxButtons.YesNo) ==
              DialogResult.Yes)
            {
                if (GetAccessoryInfo() == 1)
                {
                    if (radioGroup1.SelectedIndex == 0)
                    {
                        if (txtTenPK.Text == "" || txtNhanHieu.Text == "" || txtSoLuongTon.Text == "" || txtGiaBan.Text == "" || txtMaDT.Text == "" || txtMauSac.Text == "" || txtBaoHanh.Text == "" || cbbLoaiPK.Text == "")
                        {
                            MessageBox.Show("Chọn ảnh cho điện thoại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        }
                    }
                    else if (radioGroup1.SelectedIndex == 0 && txtLinkImage.Text != "")
                    {
                        FileStream f = new FileStream(txtLinkImage.Text, FileMode.Open, FileAccess.Read);
                        byte[] buff = null;
                        BinaryReader br = new BinaryReader(f);
                        buff = br.ReadBytes((int)f.Length);
                        ImageDTO imgdto = new ImageDTO();
                        imgdto.Image = buff;
                        ImageBL imgdal = new ImageBL();
                        imgdal.Image_Insert(imgdto);
                        GetAccessoryInfo();
                        acc.ImageID = imgdal.Image_SelectByName(buff);
                        accBL.Accessory_Insert(acc);
                        MessageBox.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtLinkImage.Text = "";
                    }
                    else if (radioGroup1.SelectedIndex == 1 && txtLinkImage.Text == "")
                    {
                        acc.ImageID = Int32.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ImageID").ToString());
                        GetAccessoryInfo();
                        accBL.Accessory_Update(acc);
                        MessageBox.Show("Sửa phụ kiện thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else if (radioGroup1.SelectedIndex == 1 && txtLinkImage.Text != "")
                    {
                        FileStream f = new FileStream(txtLinkImage.Text, FileMode.Open, FileAccess.Read);
                        byte[] buff = null;
                        BinaryReader br = new BinaryReader(f);
                        buff = br.ReadBytes((int)f.Length);
                        ImageDTO imgdto = new ImageDTO();
                        imgdto.Image = buff;
                        ImageBL imgdal = new ImageBL();
                        imgdal.Image_Insert(imgdto);
                        GetAccessoryInfo();
                        acc.ImageID = imgdal.Image_SelectByName(buff);
                        accBL.Accessory_Update(acc);
                        MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtLinkImage.Text = "";
                    }
                    else if (radioGroup1.SelectedIndex == 2)
                    {
                        accBL.Accessory_DeleteByID(acc);
                        MessageBox.Show("Xóa phụ kiện thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    accessoryTableAdapter.Fill(sellingmobilemanagementDataSet2.accessory);
                }
            }
        }
        private void txtMaDT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                e.Handled = true;
            Constraint.checkLenght(txtMaDT, e, 3);
            if ((Keys)e.KeyChar == Keys.Enter)
            {
                acc = accBL.Accessory_SelectByID(int.Parse(txtMaDT.Text));
                if (acc.Name != null)
                {
                    txtTenPK.Text = acc.Name;
                    txtNhanHieu.Text = acc.Brand;
                    txtSoLuongTon.Text = acc.Count.ToString();
                    txtGiaBan.Text = acc.Prize.ToString();
                    txtMauSac.Text = acc.Color.ToString();
                    txtBaoHanh.Text = acc.WarrantyTime.ToString();
                    cbbLoaiPK.Text = acc.Type;
                }
                else
                    MessageBox.Show("Không tìm thấy phụ kiện");
            }
        }

        private void btnCapNhatImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png";
            if (ofd.ShowDialog() == DialogResult.OK)
            {

                txtLinkImage.Text = ofd.FileName.ToString();

                FileStream f = new FileStream(txtLinkImage.Text, FileMode.Open, FileAccess.Read);
                picPhuKien.Image = resizeImage(Image.FromFile(ofd.FileName), picPhuKien.Width, picPhuKien.Height);
            }
        }

        private void txtTenPK_KeyPress(object sender, KeyPressEventArgs e)
        {
            Constraint.checkLenght(txtTenPK, e, 50);   
        }

        private void txtSoLuongTon_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                e.Handled = true;
            Constraint.checkLenght(txtSoLuongTon, e, 3);
        }

        private void txtBaoHanh_KeyPress(object sender, KeyPressEventArgs e)
        {
            Constraint.checkLenght(txtBaoHanh, e, 6);
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txtGiaBan_KeyPress(object sender, KeyPressEventArgs e)
        {
            Constraint.checkLenght(txtGiaBan, e, 8);
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txtHieu_KeyPress(object sender, KeyPressEventArgs e)
        {
            Constraint.checkLenght(txtNhanHieu, e, 50);
        }

        private void txtMau_KeyPress(object sender, KeyPressEventArgs e)
        {
            Constraint.checkLenght(txtMauSac, e, 20);
        }
    }
}
